package com.labo.erpcrm.controleur;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.labo.erpcrm.bean.LigneCommande;
import com.labo.erpcrm.metier.NouvelleVenteBusiness;

@WebServlet(name="/NouvelleVente")
public class VenteControleur extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static final String VUE_VENTE = "/new-sale.jsp";
	protected NouvelleVenteBusiness nouvelleVenteBusiness;

	public VenteControleur() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("DO GET VENTE");
		RequestDispatcher rd = null;
		//		HttpSession session = req.getSession();
		rd = getServletContext().getRequestDispatcher(VUE_VENTE);
		rd.forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("DO POST VENTE");
		RequestDispatcher rd = null;
		nouvelleVenteBusiness = new NouvelleVenteBusiness();
		ArrayList<LigneCommande> lignes = nouvelleVenteBusiness.validateNouvelleVente(req);
		if(lignes != null){
			nouvelleVenteBusiness.saveLigneCommande(lignes);
		}
		rd = getServletContext().getRequestDispatcher(VUE_VENTE);
		req.setAttribute("nouvelle_vente", nouvelleVenteBusiness);
		rd.forward(req, resp);
	}



}
